/**
 * 动态模块加载示例
 * 展示如何使用动态加载器添加新模块
 */
import { DICT_TYPE } from './types';
import { 
  loadModuleDictTypes, 
  unloadModuleDictTypes, 
  reloadModuleDictTypes,
  batchLoadModuleDictTypes,
  getModuleLoadStatus,
  isModuleLoaded
} from './loader';

// 示例：定义一个新的财务模块字典类型
export const FINANCE_DICT_TYPES = {
  // 财务模块字典类型
  PAYMENT_METHOD: 'FINANCE_PAYMENT_METHOD', // 支付方式
  INVOICE_TYPE: 'FINANCE_INVOICE_TYPE',     // 发票类型
  ACCOUNT_STATUS: 'FINANCE_ACCOUNT_STATUS',  // 账户状态
  TAX_RATE: 'FINANCE_TAX_RATE',              // 税率
} as const;

// 财务模块信息
export const FINANCE_MODULE_INFO = {
  name: 'finance',
  description: '财务模块',
  dictTypes: Object.values(FINANCE_DICT_TYPES) as unknown as DICT_TYPE[],
};

/**
 * 示例：加载财务模块
 */
export async function loadFinanceModule(): Promise<boolean> {
  return await loadModuleDictTypes(FINANCE_MODULE_INFO.name, FINANCE_MODULE_INFO);
}

/**
 * 示例：卸载财务模块
 */
export function unloadFinanceModule(): boolean {
  return unloadModuleDictTypes(FINANCE_MODULE_INFO.name);
}

/**
 * 示例：重新加载财务模块
 */
export async function reloadFinanceModule(): Promise<boolean> {
  return await reloadModuleDictTypes(FINANCE_MODULE_INFO.name, FINANCE_MODULE_INFO);
}

/**
 * 示例：检查财务模块是否已加载
 */
export function checkFinanceModuleLoaded(): boolean {
  return isModuleLoaded(FINANCE_MODULE_INFO.name);
}

/**
 * 示例：获取财务模块加载状态
 */
export function getFinanceModuleStatus(): { loaded: boolean; dictTypesCount: number } {
  const allStatus = getModuleLoadStatus();
  return allStatus[FINANCE_MODULE_INFO.name] || { loaded: false, dictTypesCount: 0 };
}

/**
 * 示例：批量加载多个模块
 */
export async function loadMultipleModules(): Promise<Record<string, boolean>> {
  // 定义另一个示例模块：人力资源模块
  const HR_DICT_TYPES = {
    EMPLOYEE_STATUS: 'HR_EMPLOYEE_STATUS',   // 员工状态
    DEPARTMENT_TYPE: 'HR_DEPARTMENT_TYPE',    // 部门类型
    POSITION_LEVEL: 'HR_POSITION_LEVEL',      // 职位级别
  } as const;

  const HR_MODULE_INFO = {
    name: 'hr',
    description: '人力资源模块',
    dictTypes: Object.values(HR_DICT_TYPES) as unknown as DICT_TYPE[],
  };

  // 批量加载模块
  return await batchLoadModuleDictTypes({
    [FINANCE_MODULE_INFO.name]: FINANCE_MODULE_INFO,
    [HR_MODULE_INFO.name]: HR_MODULE_INFO,
  });
}